//============================================================================
//
// MM     MM  6666  555555  0000   2222
// MMMM MMMM 66  66 55     00  00 22  22
// MM MMM MM 66     55     00  00     22
// MM  M  MM 66666  55555  00  00  22222  --  "A 6502 Microprocessor Emulator"
// MM     MM 66  66     55 00  00 22
// MM     MM 66  66 55  55 00  00 22
// MM     MM  6666   5555   0000  222222
//
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
//
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: M6502Hi.ins,v 1.2 2005/06/16 01:11:28 stephena Exp $
//============================================================================

/**
  Code to handle addressing modes and branch instructions for
  high compatibility emulation

  @author  Bradford W. Mott
  @version $Id: M6502Hi.ins,v 1.2 2005/06/16 01:11:28 stephena Exp $
*/

#ifndef NOTSAMEPAGE
  #define NOTSAMEPAGE(_addr1, _addr2) (((_addr1) ^ (_addr2)) & 0xff00)
#endif








































































//============================================================================
//
// MM     MM  6666  555555  0000   2222
// MMMM MMMM 66  66 55     00  00 22  22
// MM MMM MM 66     55     00  00     22
// MM  M  MM 66666  55555  00  00  22222  --  "A 6502 Microprocessor Emulator"
// MM     MM 66  66     55 00  00 22
// MM     MM 66  66 55  55 00  00 22
// MM     MM  6666   5555   0000  222222
//
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
//
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: M6502Hi.ins,v 1.2 2005/06/16 01:11:28 stephena Exp $
//============================================================================

/**
  Code and cases to emulate each of the 6502 instruction

  @author  Bradford W. Mott
  @version $Id: M6502Hi.ins,v 1.2 2005/06/16 01:11:28 stephena Exp $
*/

#ifndef NOTSAMEPAGE
  #define NOTSAMEPAGE(_addr1, _addr2) (((_addr1) ^ (_addr2)) & 0xff00)
#endif

















































































































































case 0x69:
{
  operand = peek(PC++);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x65:
{
  operand = peek(peek(PC++));
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x75:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x6d:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x7d:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x79:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x61:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x71:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;


case 0x4b:
{
  operand = peek(PC++);
}
{
  A &= operand;

  // Set carry flag according to the right-most bit
  C = A & 0x01;

  A = (A >> 1) & 0x7f;

  notZ = A;
  N = A & 0x80;
}
break;


case 0x0b:
case 0x2b:
{
  operand = peek(PC++);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
  C = N;
}
break;


case 0x29:
{
  operand = peek(PC++);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x25:
{
  operand = peek(peek(PC++));
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x35:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x2d:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x3d:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x39:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x21:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x31:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A &= operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x8b:
{
  operand = peek(PC++);
}
{
  // NOTE: The implementation of this instruction is based on
  // information from the 64doc.txt file.  This instruction is
  // reported to be unstable!
  A = (A | 0xee) & X & operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x6b:
{
  operand = peek(PC++);
}
{
  // NOTE: The implementation of this instruction is based on
  // information from the 64doc.txt file.  There are mixed
  // reports on its operation!
  if(!D)
  {
    A &= operand;
    A = ((A >> 1) & 0x7f) | (C ? 0x80 : 0x00);

    C = A & 0x40;
    V = (A & 0x40) ^ ((A & 0x20) << 1);

    notZ = A;
    N = A & 0x80;
  }
  else
  {
    uint8_t value = A & operand;

    A = ((value >> 1) & 0x7f) | (C ? 0x80 : 0x00);
    N = C;
    notZ = A;
    V = (value ^ A) & 0x40;

    if(((value & 0x0f) + (value & 0x01)) > 0x05)
    {
      A = (A & 0xf0) | ((A + 0x06) & 0x0f);
    }

    if(((value & 0xf0) + (value & 0x10)) > 0x50)
    {
      A = (A + 0x60) & 0xff;
      C = 1;
    }
    else
    {
      C = 0;
    }
  }
}
break;


case 0x0a:
{
  peek(PC);
}
{
  // Set carry flag according to the left-most bit in A
  C = A & 0x80;

  A <<= 1;

  notZ = A;
  N = A & 0x80;
}
break;

case 0x06:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x16:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x0e:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x1e:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;


case 0x90:
{
  operand = peek(PC++);
}
{
  if(!C)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0xb0:
{
  operand = peek(PC++);
}
{
  if(C)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0xf0:
{
  operand = peek(PC++);
}
{
  if(!notZ)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0x24:
{
  operand = peek(peek(PC++));
}
{
  notZ = (A & operand);
  N = operand & 0x80;
  V = operand & 0x40;
}
break;

case 0x2C:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  notZ = (A & operand);
  N = operand & 0x80;
  V = operand & 0x40;
}
break;


case 0x30:
{
  operand = peek(PC++);
}
{
  if(N)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0xD0:
{
  operand = peek(PC++);
}
{
  if(notZ)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0x10:
{
  operand = peek(PC++);
}
{
  if(!N)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0x00:
{
  peek(PC++);

  B = true;

  poke(0x0100 + SP--, PC >> 8);
  poke(0x0100 + SP--, PC & 0x00ff);
  poke(0x0100 + SP--, PS());

  I = true;

  PC = peek(0xfffe);
  PC |= ((uint16_t)peek(0xffff) << 8);
}
break;


case 0x50:
{
  operand = peek(PC++);
}
{
  if(!V)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0x70:
{
  operand = peek(PC++);
}
{
  if(V)
  {
    peek(PC);
    uint16_t address = PC + (int8_t)operand;
    if(NOTSAMEPAGE(PC, address))
      peek((PC & 0xFF00) | (address & 0x00FF));
    PC = address;
  }
}
break;


case 0x18:
{
  peek(PC);
}
{
  C = false;
}
break;


case 0xd8:
{
  peek(PC);
}
{
  D = false;
}
break;


case 0x58:
{
  peek(PC);
}
{
  I = false;
}
break;


case 0xb8:
{
  peek(PC);
}
{
  V = false;
}
break;


case 0xc9:
{
  operand = peek(PC++);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xc5:
{
  operand = peek(peek(PC++));
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xd5:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xcd:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xdd:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xd9:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xc1:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xd1:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  uint16_t value = (uint16_t)A - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;


case 0xe0:
{
  operand = peek(PC++);
}
{
  uint16_t value = (uint16_t)X - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xe4:
{
  operand = peek(peek(PC++));
}
{
  uint16_t value = (uint16_t)X - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xec:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  uint16_t value = (uint16_t)X - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;


case 0xc0:
{
  operand = peek(PC++);
}
{
  uint16_t value = (uint16_t)Y - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xc4:
{
  operand = peek(peek(PC++));
}
{
  uint16_t value = (uint16_t)Y - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;

case 0xcc:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  uint16_t value = (uint16_t)Y - (uint16_t)operand;

  notZ = value;
  N = value & 0x0080;
  C = !(value & 0x0100);
}
break;


case 0xcf:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;

case 0xdf:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;

case 0xdb:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;

case 0xc7:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;

case 0xd7:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;

case 0xc3:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;

case 0xd3:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  uint16_t value2 = (uint16_t)A - (uint16_t)value;
  notZ = value2;
  N = value2 & 0x0080;
  C = !(value2 & 0x0100);
}
break;


case 0xc6:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;

case 0xd6:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;

case 0xce:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;

case 0xde:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand - 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;


case 0xca:
{
  peek(PC);
}
{
  X--;

  notZ = X;
  N = X & 0x80;
}
break;


case 0x88:
{
  peek(PC);
}
{
  Y--;

  notZ = Y;
  N = Y & 0x80;
}
break;


case 0x49:
{
  operand = peek(PC++);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x45:
{
  operand = peek(peek(PC++));
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x55:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x4d:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x5d:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x59:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x41:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x51:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0xe6:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand + 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;

case 0xf6:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand + 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;

case 0xee:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand + 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;

case 0xfe:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = operand + 1;
  poke(operandAddress, value);

  notZ = value;
  N = value & 0x80;
}
break;


case 0xe8:
{
  peek(PC);
}
{
  X++;
  notZ = X;
  N = X & 0x80;
}
break;


case 0xc8:
{
  peek(PC);
}
{
  Y++;
  notZ = Y;
  N = Y & 0x80;
}
break;


case 0xef:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xff:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xfb:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xe7:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xf7:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xe3:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xf3:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  operand = operand + 1;
  poke(operandAddress, operand);

  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;


case 0x4c:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
}
{
  PC = operandAddress;
}
break;

case 0x6c:
{
  uint16_t addr = peek(PC++);
  addr |= ((uint16_t)peek(PC++) << 8);

  // Simulate the error in the indirect addressing mode!
  uint16_t high = NOTSAMEPAGE(addr, addr + 1) ? (addr & 0xff00) : (addr + 1);

  operandAddress = peek(addr);
  operandAddress |= ((uint16_t)peek(high) << 8);
}
{
  PC = operandAddress;
}
break;


case 0x20:
{
  uint8_t low = peek(PC++);
  peek(0x0100 + SP);

  // It seems that the 650x does not push the address of the next instruction
  // on the stack it actually pushes the address of the next instruction
  // minus one.  This is compensated for in the RTS instruction
  poke(0x0100 + SP--, PC >> 8);
  poke(0x0100 + SP--, PC & 0xff);

  PC = low | ((uint16_t)peek(PC++) << 8);
}
break;


case 0xbb:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A = X = SP = SP & operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0xaf:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  A = operand;
  X = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xbf:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A = operand;
  X = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xa7:
{
  operand = peek(peek(PC++));
}
{
  A = operand;
  X = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xb7:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += Y;
  operand = peek(address);
}
{
  A = operand;
  X = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xa3:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  A = operand;
  X = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xb3:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A = operand;
  X = operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0xa9:
{
  operand = peek(PC++);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xa5:
{
  operand = peek(peek(PC++));
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xb5:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xad:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xbd:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xb9:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xa1:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0xb1:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A = operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0xa2:
{
  operand = peek(PC++);
}
{
  X = operand;
  notZ = X;
  N = X & 0x80;
}
break;

case 0xa6:
{
  operand = peek(peek(PC++));
}
{
  X = operand;
  notZ = X;
  N = X & 0x80;
}
break;

case 0xb6:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += Y;
  operand = peek(address);
}
{
  X = operand;
  notZ = X;
  N = X & 0x80;
}
break;

case 0xae:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  X = operand;
  notZ = X;
  N = X & 0x80;
}
break;

case 0xbe:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  X = operand;
  notZ = X;
  N = X & 0x80;
}
break;


case 0xa0:
{
  operand = peek(PC++);
}
{
  Y = operand;
  notZ = Y;
  N = Y & 0x80;
}
break;

case 0xa4:
{
  operand = peek(peek(PC++));
}
{
  Y = operand;
  notZ = Y;
  N = Y & 0x80;
}
break;

case 0xb4:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  Y = operand;
  notZ = Y;
  N = Y & 0x80;
}
break;

case 0xac:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  Y = operand;
  notZ = Y;
  N = Y & 0x80;
}
break;

case 0xbc:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  Y = operand;
  notZ = Y;
  N = Y & 0x80;
}
break;


case 0x4a:
{
  peek(PC);
}
{
  // Set carry flag according to the right-most bit
  C = A & 0x01;

  A = (A >> 1) & 0x7f;

  notZ = A;
  N = A & 0x80;
}
break;


case 0x46:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x56:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x4e:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x5e:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;


case 0xab:
{
  operand = peek(PC++);
}
{
  // NOTE: The implementation of this instruction is based on
  // information from the 64doc.txt file.  This instruction is
  // reported to be very unstable!
  A = X = (A | 0xee) & operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x1a:
case 0x3a:
case 0x5a:
case 0x7a:
case 0xda:
case 0xea:
case 0xfa:
{
  peek(PC);
}
{
}
break;

case 0x80:
case 0x82:
case 0x89:
case 0xc2:
case 0xe2:
{
  operand = peek(PC++);
}
{
}
break;

case 0x04:
case 0x44:
case 0x64:
{
  operand = peek(peek(PC++));
}
{
}
break;

case 0x14:
case 0x34:
case 0x54:
case 0x74:
case 0xd4:
case 0xf4:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
}
break;

case 0x0c:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
}
break;

case 0x1c:
case 0x3c:
case 0x5c:
case 0x7c:
case 0xdc:
case 0xfc:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
}
break;


case 0x09:
{
  operand = peek(PC++);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x05:
{
  operand = peek(peek(PC++));
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x15:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x0d:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x1d:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x19:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x01:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x11:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x48:
{
  peek(PC);
}
{
  poke(0x0100 + SP--, A);
}
break;


case 0x08:
{
  peek(PC);
}
{
  poke(0x0100 + SP--, PS());
}
break;


case 0x68:
{
  peek(PC);
}
{
  peek(0x0100 + SP++);
  A = peek(0x0100 + SP);
  notZ = A;
  N = A & 0x80;
}
break;


case 0x28:
{
  peek(PC);
}
{
  peek(0x0100 + SP++);
  PS(peek(0x0100 + SP));
}
break;


case 0x2f:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x3f:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x3b:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x27:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x37:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x23:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x33:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t value = (operand << 1) | (C ? 1 : 0);
  poke(operandAddress, value);

  A &= value;
  C = operand & 0x80;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x2a:
{
  peek(PC);
}
{
  bool oldC = C;

  // Set carry flag according to the left-most bit
  C = A & 0x80;

  A = (A << 1) | (oldC ? 1 : 0);

  notZ = A;
  N = A & 0x80;
}
break;


case 0x26:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the left-most bit in operand
  C = operand & 0x80;

  operand = (operand << 1) | (oldC ? 1 : 0);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x36:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the left-most bit in operand
  C = operand & 0x80;

  operand = (operand << 1) | (oldC ? 1 : 0);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x2e:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the left-most bit in operand
  C = operand & 0x80;

  operand = (operand << 1) | (oldC ? 1 : 0);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x3e:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the left-most bit in operand
  C = operand & 0x80;

  operand = (operand << 1) | (oldC ? 1 : 0);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;


case 0x6a:
{
  peek(PC);
}
{
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = A & 0x01;

  A = ((A >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);

  notZ = A;
  N = A & 0x80;
}
break;

case 0x66:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x76:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x6e:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;

case 0x7e:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  notZ = operand;
  N = operand & 0x80;
}
break;


case 0x6f:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x7f:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x7b:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x67:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x77:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x63:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0x73:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  uint8_t oldA = A;
  bool oldC = C;

  // Set carry flag according to the right-most bit
  C = operand & 0x01;

  operand = ((operand >> 1) & 0x7f) | (oldC ? 0x80 : 0x00);
  poke(operandAddress, operand);

  if(!D)
  {
    int16_t sum = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((sum > 127) || (sum < -128));

    sum = (int16_t)A + (int16_t)operand + (C ? 1 : 0);
    A = sum;
    C = (sum > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t sum = ourBCDTable[0][A] + ourBCDTable[0][operand] + (C ? 1 : 0);

    C = (sum > 99);
    A = ourBCDTable[1][sum & 0xff];
    notZ = A;
    N = A & 0x80;
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;


case 0x40:
{
  peek(PC);
}
{
  peek(0x0100 + SP++);
  PS(peek(0x0100 + SP++));
  PC = peek(0x0100 + SP++);
  PC |= ((uint16_t)peek(0x0100 + SP) << 8);
}
break;


case 0x60:
{
  peek(PC);
}
{
  peek(0x0100 + SP++);
  PC = peek(0x0100 + SP++);
  PC |= ((uint16_t)peek(0x0100 + SP) << 8);
  peek(PC++);
}
break;


case 0x8f:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
}
{
  poke(operandAddress, A & X);
}
break;

case 0x87:
{
  operandAddress = peek(PC++);
}
{
  poke(operandAddress, A & X);
}
break;

case 0x97:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + Y) & 0xFF;
}
{
  poke(operandAddress, A & X);
}
break;

case 0x83:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
}
{
  poke(operandAddress, A & X);
}
break;


case 0xe9:
case 0xeb:
{
  operand = peek(PC++);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xe5:
{
  operand = peek(peek(PC++));
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xf5:
{
  uint8_t address = peek(PC++);
  peek(address);
  address += X;
  operand = peek(address);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xed:
{
  uint16_t address = peek(PC++);
  address |= ((uint16_t)peek(PC++) << 8);
  operand = peek(address);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xfd:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + X));
  if((low + X) > 0xFF)
    operand = peek((high | low) + X);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xf9:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xe1:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  uint16_t address = peek(pointer++);
  address |= ((uint16_t)peek(pointer) << 8);
  operand = peek(address);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;

case 0xf1:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  operand = peek(high | (uint8_t)(low + Y));
  if((low + Y) > 0xFF)
    operand = peek((high | low) + Y);
}
{
  uint8_t oldA = A;

  if(!D)
  {
    operand = ~operand;
    int16_t difference = (int16_t)((int8_t)A) + (int16_t)((int8_t)operand) + (C ? 1 : 0);
    V = ((difference > 127) || (difference < -128));

    difference = ((int16_t)A) + ((int16_t)operand) + (C ? 1 : 0);
    A = difference;
    C = (difference > 0xff);
    notZ = A;
    N = A & 0x80;
  }
  else
  {
    int16_t difference = ourBCDTable[0][A] - ourBCDTable[0][operand]
        - (C ? 0 : 1);

    if(difference < 0)
      difference += 100;

    A = ourBCDTable[1][difference];
    notZ = A;
    N = A & 0x80;

    C = (oldA >= (operand + (C ? 0 : 1)));
    V = ((oldA ^ A) & 0x80) && ((A ^ operand) & 0x80);
  }
}
break;


case 0xcb:
{
  operand = peek(PC++);
}
{
  uint16_t value = (uint16_t)(X & A) - (uint16_t)operand;
  X = (value & 0xff);

  notZ = X;
  N = X & 0x80;
  C = !(value & 0x0100);
}
break;


case 0x38:
{
  peek(PC);
}
{
  C = true;
}
break;


case 0xf8:
{
  peek(PC);
}
{
  D = true;
}
break;


case 0x78:
{
  peek(PC);
}
{
  I = true;
}
break;


case 0x9f:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
}
{
  // NOTE: There are mixed reports on the actual operation
  // of this instruction!
  poke(operandAddress, A & X & (((operandAddress >> 8) & 0xff) + 1));
}
break;

case 0x93:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
}
{
  // NOTE: There are mixed reports on the actual operation
  // of this instruction!
  poke(operandAddress, A & X & (((operandAddress >> 8) & 0xff) + 1));
}
break;


case 0x9b:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
}
{
  // NOTE: There are mixed reports on the actual operation
  // of this instruction!
  SP = A & X;
  poke(operandAddress, A & X & (((operandAddress >> 8) & 0xff) + 1));
}
break;


case 0x9e:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
}
{
  // NOTE: There are mixed reports on the actual operation
  // of this instruction!
  poke(operandAddress, X & (((operandAddress >> 8) & 0xff) + 1));
}
break;


case 0x9c:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
}
{
  // NOTE: There are mixed reports on the actual operation
  // of this instruction!
  poke(operandAddress, Y & (((operandAddress >> 8) & 0xff) + 1));
}
break;


case 0x0f:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x1f:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x1b:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x07:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x17:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x03:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x13:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the left-most bit in value
  C = operand & 0x80;

  operand <<= 1;
  poke(operandAddress, operand);

  A |= operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x4f:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x5f:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x5b:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x47:
{
  operandAddress = peek(PC++);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x57:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x43:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;

case 0x53:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
  operand = peek(operandAddress);
  poke(operandAddress, operand);
}
{
  // Set carry flag according to the right-most bit in value
  C = operand & 0x01;

  operand = (operand >> 1) & 0x7f;
  poke(operandAddress, operand);

  A ^= operand;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x85:
{
  operandAddress = peek(PC++);
}
{
  poke(operandAddress, A);
}
break;

case 0x95:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
}
{
  poke(operandAddress, A);
}
break;

case 0x8d:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
}
{
  poke(operandAddress, A);
}
break;

case 0x9d:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + X));
  operandAddress = (high | low) + X;
}
{
  poke(operandAddress, A);
}
break;

case 0x99:
{
  uint16_t low = peek(PC++);
  uint16_t high = ((uint16_t)peek(PC++) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
}
{
  poke(operandAddress, A);
}
break;

case 0x81:
{
  uint8_t pointer = peek(PC++);
  peek(pointer);
  pointer += X;
  operandAddress = peek(pointer++);
  operandAddress |= ((uint16_t)peek(pointer) << 8);
}
{
  poke(operandAddress, A);
}
break;

case 0x91:
{
  uint8_t pointer = peek(PC++);
  uint16_t low = peek(pointer++);
  uint16_t high = ((uint16_t)peek(pointer) << 8);
  peek(high | (uint8_t)(low + Y));
  operandAddress = (high | low) + Y;
}
{
  poke(operandAddress, A);
}
break;


case 0x86:
{
  operandAddress = peek(PC++);
}
{
  poke(operandAddress, X);
}
break;

case 0x96:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + Y) & 0xFF;
}
{
  poke(operandAddress, X);
}
break;

case 0x8e:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
}
{
  poke(operandAddress, X);
}
break;


case 0x84:
{
  operandAddress = peek(PC++);
}
{
  poke(operandAddress, Y);
}
break;

case 0x94:
{
  operandAddress = peek(PC++);
  peek(operandAddress);
  operandAddress = (operandAddress + X) & 0xFF;
}
{
  poke(operandAddress, Y);
}
break;

case 0x8c:
{
  operandAddress = peek(PC++);
  operandAddress |= ((uint16_t)peek(PC++) << 8);
}
{
  poke(operandAddress, Y);
}
break;


case 0xaa:
{
  peek(PC);
}
{
  X = A;
  notZ = X;
  N = X & 0x80;
}
break;


case 0xa8:
{
  peek(PC);
}
{
  Y = A;
  notZ = Y;
  N = Y & 0x80;
}
break;


case 0xba:
{
  peek(PC);
}
{
  X = SP;
  notZ = X;
  N = X & 0x80;
}
break;


case 0x8a:
{
  peek(PC);
}
{
  A = X;
  notZ = A;
  N = A & 0x80;
}
break;


case 0x9a:
{
  peek(PC);
}
{
  SP = X;
}
break;


case 0x98:
{
  peek(PC);
}
{
  A = Y;
  notZ = A;
  N = A & 0x80;
}
break;
